package org.lookouster.nextAgadir.pointageBureau.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.lookouster.nextAgadir.pointageBureau.bases.Billon;
import org.lookouster.nextAgadir.pointageBureau.bases.EspaceTravail;
import org.lookouster.nextAgadir.pointageBureau.bases.Ferme;
import org.lookouster.nextAgadir.pointageBureau.bases.LocalisationGPS;
import org.lookouster.nextAgadir.pointageBureau.bases.Secteur;
import org.lookouster.nextAgadir.pointageBureau.bases.Serre;
import org.lookouster.nextAgadir.pointageBureau.outils.BDConnect;
import org.lookouster.nextAgadir.pointageBureau.outils.BDFunctions;
import org.lookouster.nextAgadir.pointageBureau.outils.functions;

/*
 * NewChamp.java
 *
 * Created on 25 avril 2008, 02:29
 */


/**
 *
 * @author  Lookouster
 */
public class NewField extends javax.swing.JDialog {
	
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnValider;
    private javax.swing.JButton btnAnnuler;
    private javax.swing.JComboBox typeChamp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    //private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;//Label ferme
    private javax.swing.JLabel jLabel8;//Label secteur
    private javax.swing.JLabel jLabel9;//label Serre
    private javax.swing.JLabel jLabel10;//label Billon
    private javax.swing.JList listFermes;
    private javax.swing.JList listSerres;
    private javax.swing.JList listSecteurs;
    private javax.swing.JList listBillons;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    /*private javax.swing.JSpinner spnGPS1;
    private javax.swing.JSpinner spnGPS2;
    private javax.swing.JSpinner spnGPS3;
    private javax.swing.JSpinner spnGPS4;*/
    private javax.swing.JTextArea txtDesc;
    private javax.swing.JTextArea txtAdr;
    private javax.swing.JTextField txtDesignation;
    private List<Ferme> lFermes;
    private List<Serre> lSerres;
    private List<Secteur> lSecteurs;
    private List<Billon> lBillons;
    // End of variables declaration   
    private int mode;
    
    /** Creates new form NewChamp */
    /**
     * mode 1 --> New Champ
     * mode 2 --> Display info on Champ
     * mode 3 --> Edit Champ
     * mode 4 --> Delete Champ
     */
    public NewField(java.awt.Frame parent, int mode) {
        super(parent, true);
        this.mode=mode;
        initComponents();
        if(mode==1)
        	init4New();
        else if(mode==2)
        	init4Display();
        else if(mode==3)
        	init4Edit();
        else if(mode==4)
        	init4Delete();
    }
    
    
    public void init4New()
    {
    	jLabel10.setEnabled(false);
    	jScrollPane6.setEnabled(false);
    	listBillons.setEnabled(false);
    	btnValider.setText(UICommons.btnadd);
    	typeChamp.addActionListener(new ActionListener()
        {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				listenChampChoice1();
			}
        	
        });
    	
    }
    
    public void init4Display()
    {
    	jLabel1.setText(UICommons.flblShowChpTitle);
    	btnValider.setText(UICommons.btnEdit);
    	
    	txtDesignation.setEnabled(false);
    	txtDesc.setEnabled(false);
    	txtAdr.setEnabled(false);
    	typeChamp.addActionListener(new ActionListener()
        {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				listenChampChoice2();
			}
        	
        });
    }
    
    public void init4Edit()
    {
    	jLabel1.setText(UICommons.flblEditChpTitle);
    	btnValider.setText(UICommons.btnValid);
    	btnAnnuler.setText(UICommons.btnCancel);
    	txtDesc.setEnabled(true);
    	txtDesignation.setEnabled(true);
    	if(typeChamp.getSelectedIndex()==0)
    		txtAdr.setEnabled(true);
    	typeChamp.setEnabled(false);
    	    	
    	
    }
    
    public void init4Delete()
    {
    	listenListSelectionMode2();
    	secteurView();
    	init4Display();
    	jLabel1.setText(UICommons.flblDelChpTitle);
    	btnValider.setText(UICommons.btnDel);
    	
    	
    }
                            
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel(UICommons.flblNewChpTitle);
        jLabel2 = new javax.swing.JLabel(UICommons.flblNewChpDesi);
        jLabel3 = new javax.swing.JLabel(UICommons.flblNewChpDesc);
        txtDesignation = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
    /*    spnGPS1 = new javax.swing.JSpinner();
        spnGPS3 = new javax.swing.JSpinner();
        spnGPS4 = new javax.swing.JSpinner();
        spnGPS2 = new javax.swing.JSpinner();*/
        typeChamp = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel(UICommons.flblNewChpTyp);
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDesc = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAdr = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel(UICommons.flblNewChpAdr);
        jScrollPane3 = new javax.swing.JScrollPane();
        listFermes = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        listSerres = new javax.swing.JList();
        jScrollPane5 = new javax.swing.JScrollPane();
        jScrollPane6 = new javax.swing.JScrollPane();
        listSecteurs = new javax.swing.JList();
        listBillons = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel(UICommons.flblNewChp0);
        jLabel8 = new javax.swing.JLabel(UICommons.flblNewChp1);
        jLabel9 = new javax.swing.JLabel(UICommons.flblNewChp2);
        jLabel10 = new javax.swing.JLabel(UICommons.flblNewChp3);
        //jLabel6 = new javax.swing.JLabel(UICommons.flblNewChpGPS);
        btnValider = new javax.swing.JButton(UICommons.btnValid);
        btnAnnuler = new javax.swing.JButton(UICommons.btnClean);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        

        
       
        btnAnnuler.setText(UICommons.btnClose);

        /*javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(spnGPS3)
                    .addComponent(spnGPS1, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(spnGPS4)
                    .addComponent(spnGPS2, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spnGPS3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spnGPS2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spnGPS1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spnGPS4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );*/

        typeChamp.setModel(
        		new javax.swing.DefaultComboBoxModel(UICommons.fTypeFields));
        
        
        typeChamp.setSelectedIndex(UICommons.fermPos);
        
        listFermes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listSerres.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listSecteurs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listBillons.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        if(mode==1)
        {
        	listenListSelectionMode1();
            fermeView();
        }
        
        
        if(mode==2)
        {
        	//listenListSelectionMode1();
        	listenListSelectionMode2();
        	functions.affiche("mode2");
        	secteurView();
        }
        
        
        
        loadFermes();
               

        

        txtDesc.setColumns(20);
        txtDesc.setRows(5);
        
        jScrollPane1.setViewportView(txtDesc);

        

        txtAdr.setColumns(20);
        txtAdr.setRows(5);
        
        jScrollPane2.setViewportView(txtAdr);

        
        jScrollPane2.setVisible(true);
        jLabel5.setVisible(true);
             
        jScrollPane4.setViewportView(listSerres);

        
        jScrollPane3.setViewportView(listFermes);
        
        
        jScrollPane5.setViewportView(listSecteurs);
        
        jScrollPane6.setViewportView(listBillons);

       
       
       btnAnnuler.addActionListener(new ActionListener()
       {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			listen2BtnAnnuler();
		}
    	   
       });
       
       btnValider.addActionListener(new ActionListener()
       {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			listen2BtnValider();
		}
    	   
       });
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 559, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            /*.addComponent(jLabel7)*/)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addComponent(jLabel7)
                                .addGap(145, 145, 145)
                                .addComponent(jLabel8)
                                .addGap(140, 140, 140))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(typeChamp, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addGap(60, 60, 60)
                                            .addComponent(txtDesignation, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))))
                                .addGap(43, 43, 43)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(140, 140, 140)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                /*.addComponent(jLabel6)*/
                                .addGap(43, 43, 43))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(70, 70, 70)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(47, 47, 47)
                                            /*.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)*/)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(145, 145, 145)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(99, 99, 99)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(99, 99, 99)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        
                        .addGap(99, 99, 99)
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(47, 47, 47))
            .addGroup(layout.createSequentialGroup()
                .addGap(284, 284, 284)
                .addComponent(btnValider, 100, javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(btnAnnuler, 100, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(219, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {typeChamp, txtDesignation});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtDesignation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(typeChamp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    /*.addComponent(jLabel6)*/)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        /*.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)*/))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnValider, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAnnuler, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49))
        );

        pack();
    }                      

                                             
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewField dialog = new NewField(new javax.swing.JFrame(), 1);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    
    /**
     * Listen to the selected option in the typeChamp comboBox when mode==1
     */
    public void listenChampChoice1()
    {
    	if(typeChamp.getSelectedIndex()==UICommons.fermPos)
    	{
    		//It's a ferme!! we gotta hide all lists!!
    		fermeView();
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.SectPos)
    	{
    		//functions.affiche("sct");
    		secteurView();
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.SerrPos)
    	{
    		functions.affiche("srr");
    		serreView();
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.BillPos)
    	{
    		billonView();
    	}
    	//loadFermes();
    }
    
    
    /**
     * Listen to the selected option in the typeChamp comboBox when mode==2
     */
    public void listenChampChoice2()
    {
    	if(typeChamp.getSelectedIndex()==UICommons.fermPos)
    	{
    		secteurView();
    		jScrollPane2.setVisible(true);
    		txtAdr.setVisible(true);
    		jLabel5.setVisible(true);
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.SectPos)
    	{
    		serreView();
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.SerrPos)
    	{
    		billonView();
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.BillPos)
    	{
    		afterBillonView();
    	}
    	//loadFermes();
    }
    
    
    /**
     * Following Views methods are used only in mode 1, while adding a new champ!!
     */
    public void fermeView()
    {
    	jScrollPane2.setVisible(true);
    	jLabel5.setVisible(true);
    	jLabel7.setEnabled(false);
		jLabel8.setEnabled(false);
		jLabel9.setEnabled(false);
		jLabel10.setEnabled(false);
		listFermes.setEnabled(false);
		listSerres.setEnabled(false);
		listSecteurs.setEnabled(false);
		listBillons.setEnabled(false);
		jScrollPane3.setEnabled(false);
		jScrollPane4.setEnabled(false);
		jScrollPane5.setEnabled(false);
		jScrollPane6.setEnabled(false);
    }
    
    public void secteurView()
    {
    	jScrollPane2.setVisible(false);
    	jLabel5.setVisible(false);
    	jLabel7.setVisible(true);
		jLabel8.setEnabled(false);
		jLabel9.setEnabled(false);
		jLabel10.setEnabled(false);
		listFermes.setEnabled(true);
		listSerres.setEnabled(false);
		listSecteurs.setEnabled(false);
		listBillons.setEnabled(false);
		jScrollPane3.setEnabled(true);
		jScrollPane4.setEnabled(false);
		jScrollPane5.setEnabled(false);
		jScrollPane6.setEnabled(false);
    }
    
    public void serreView()
    {
    	jScrollPane2.setVisible(false);
    	jLabel5.setVisible(false);
    	jLabel7.setEnabled(true);
		jLabel8.setEnabled(true);
		jLabel9.setEnabled(false);
		jLabel10.setEnabled(false);
		listFermes.setEnabled(true);
		listSecteurs.setEnabled(true);
		listSerres.setEnabled(false);
		listBillons.setEnabled(false);
		jScrollPane3.setEnabled(true);
		jScrollPane4.setEnabled(true);
		jScrollPane5.setEnabled(false);
		jScrollPane6.setEnabled(false);
    }
    
    public void billonView()
    {
    	jScrollPane2.setVisible(false);
    	jLabel5.setVisible(false);
    	jLabel7.setEnabled(true);
		jLabel8.setEnabled(true);
		jLabel9.setEnabled(true);
		jLabel10.setEnabled(false);
		listFermes.setEnabled(true);
		listSerres.setEnabled(true);
		listSecteurs.setEnabled(true);
		listBillons.setEnabled(false);
		jScrollPane3.setEnabled(true);
		jScrollPane4.setEnabled(true);
		jScrollPane5.setEnabled(true);
		jScrollPane6.setEnabled(false);
    }
    
    public void afterBillonView()
    {
    	jScrollPane2.setVisible(false);
    	jLabel5.setVisible(false);
    	jLabel7.setEnabled(true);
		jLabel8.setEnabled(true);
		jLabel9.setEnabled(true);
		jLabel10.setEnabled(true);
		listFermes.setEnabled(true);
		listSerres.setEnabled(true);
		listSecteurs.setEnabled(true);
		listBillons.setEnabled(true);
		jScrollPane3.setEnabled(true);
		jScrollPane4.setEnabled(true);
		jScrollPane5.setEnabled(true);
		jScrollPane6.setEnabled(true);
    }
    
    /**
     * Called at init when selected type champ different from "ferme"
     * This will load ferme's list in the uI
     */
    public void loadFermes()
    {
    	listSerres.setModel(new DefaultListModel());
    	listSecteurs.setModel(new DefaultListModel());
    	listBillons.setModel(new DefaultListModel());
    	loadFermes3();
    }
    /**
     * Called in mode 3; no need to empty following list in fact
     */
    public void loadFermes3()
    {
    	//functions.affiche("yo1!!");
    	try {
			lFermes=BDFunctions.getFermesList();
			//functions.affiche("yo2!!");
			listFermes.setModel(new javax.swing.AbstractListModel() {
	            String[] fermes = functions.buildFListContent(lFermes);
	            public int getSize() { return fermes.length; }
	            public Object getElementAt(int i) { return fermes[i]; }
	        });
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			functions.affiche("unable to collect fermes list: "+e.getMessage());
		}
    }
    
    /**
     * Is called when a Secteur item is selected in listFermes
     * Display related serres list
     */
    public void loadSerres()
    {
    	
    	listSerres.setModel(new DefaultListModel());
    	
    	listBillons.setModel(new DefaultListModel());
    	loadSerres3();
    	
    }
    
    /**
     * Same as loadFermes3
     */
    public void loadSerres3()
    {
    	if(listSecteurs.getSelectedIndex()!=-1)
    	{
    		int pos=listSecteurs.getSelectedIndex();
        	/*System.out.println("ui="+listSecteurs.getSelectedIndices().length+"\n"+
        			"mem="+lSecteurs.size());*/
        	if(lSecteurs.get(pos).size()!=0)
        	{
        		lSerres=new ArrayList<Serre>(
            			lSecteurs.get(pos).getSerres());
            	listSerres.setModel(new javax.swing.AbstractListModel() {
                    String[] serres = functions.buildSrListContent(lSerres);
                    public int getSize() { return serres.length; }
                    public Object getElementAt(int i) { return serres[i]; }
                });
        	}
    	}
    	

    }
    
    
    /**
     * Is called when a Ferme item is selected in listFermes
     * Display related secteurs
     */
    public void loadSecteurs()
    {
    	listSecteurs.setModel(new DefaultListModel());
    	listSerres.setModel(new DefaultListModel());
    	listBillons.setModel(new DefaultListModel());
    	loadSecteurs3();
    }
    
    /**
     * Same as loadFermes3()
     */
    public void loadSecteurs3()
    {
    	//functions.affiche("shd load sect?");
    	if(listFermes.getSelectedIndex()!=-1)
    	{
    		int pos=listFermes.getSelectedIndex();
        	if(lFermes.get(pos).size()!=0)
        	{
        		//System.out.print(lFermes.get(pos).size());
        		//functions.affiche( "secteurs");
        		lSecteurs=new ArrayList<Secteur>(
            			lFermes.get(pos).getSecteurs());
            	
            	listSecteurs.setModel(new javax.swing.AbstractListModel() {
                    String[] secteurs = functions.buildScListContent(lSecteurs);
                    public int getSize() { return secteurs.length; }
                    public Object getElementAt(int i) { return secteurs[i]; }
                });
        	}
        	
    	}
    	
    	
    	
    }
    
    /**
     * Is called when a Secteur item is selected in listSecteurs
     * Display related billons
     */
    public void loadBillons()
    {
    	listBillons.setModel(new DefaultListModel());
    	//functions.affiche("loadBil");
    	if(listSerres.getSelectedIndex()!=-1)
    	{
    		int pos=listSerres.getSelectedIndex();
        	if(lSerres.get(pos).size()!=0)
        	{
        		lBillons=new ArrayList<Billon>(
            			lSerres.get(pos).getBillons());
            	
            	listBillons.setModel(new javax.swing.AbstractListModel() {
                    String[] billons = functions.buildBListContent(lBillons);
                    public int getSize() { return billons.length; }
                    public Object getElementAt(int i) { return billons[i]; }
                });
        	}
        	else
        		functions.affiche("walo");
    	}
    	
    	
    	
    }
    
    
    /**
     * Listen to btnValdier's click
     */
    public void listen2BtnValider()
    {
    	if(mode==1)
    		saveThis();
    	else if(mode==2)
    		showThis();
    	else if(mode==3)
    		editThis();
    	else if(mode==4)
    		deleteThis();
    	
    }
    
    /**
     * Listen to btnAnnuler click
     */
    public void listen2BtnAnnuler()
    {
    	if(mode==3)
    	{
    		mode=2;
        	emptyFields();
        	typeChamp.setSelectedIndex(UICommons.fermPos);
        	typeChamp.setEnabled(true);
        	removeListListeners();
        	listenListSelectionMode2();
        	//serreView();
        	init4Display();
        	loadFermes();
        	releaseLists();
    	}
    	else
    	{
    		this.setVisible(false);
    	}
    		
    }
    
    /**
     * Executed method for saving entered data on a champ
     * when btnValider is clicked
     * @throws Exception 
     */
    public void saveThis()
    {
    	if(existEmptyFields())
    	{
    		JOptionPane.showMessageDialog(this,UICommons.notifEmpty);
    		return;
    	}
    	
    	//A new Ferme option is choosen
    	if(typeChamp.getSelectedIndex()==UICommons.fermPos)
    	{
    		
			try {
				BDConnect bdc = new BDConnect();
				Ferme tmp=new Ferme(txtDesignation.getText().trim(),
						  txtAdr.getText().trim(),
	    				  txtDesc.getText().trim(),
	    				  new ArrayList<LocalisationGPS>());
				tmp.saveDB(bdc);
	    		bdc.shutdown();
	    		if(lFermes==null)
	    			lFermes=new ArrayList<Ferme>();
	    		lFermes.add(tmp);
	    		loadFermes();
	    		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb ferme saveThis(): "+e.getMessage());
				
			}
    		
    	}
    	//We want to add a "Serre"
    	if(typeChamp.getSelectedIndex()==UICommons.SerrPos)
    	{
    		if(listSecteurs.isSelectionEmpty())
    		{
    			JOptionPane.showMessageDialog(this,UICommons.notifSecteur);
        		return;
    		}
    		try {
				BDConnect bdc=new BDConnect();
				Serre tmp=new Serre(lSecteurs.get(listSecteurs.getSelectedIndex()),
						  txtDesignation.getText().trim(),
						  txtDesc.getText().trim(),
						  new ArrayList<LocalisationGPS>());
				tmp.saveDB(bdc);
				bdc.shutdown();
				
				lSecteurs.get(listSecteurs.getSelectedIndex()).addSerre(tmp);
				loadSerres();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb serre saveThis(): "+e.getMessage());
			}
    	}
    	
    	//New Secteur option choosen
    	if(typeChamp.getSelectedIndex()==UICommons.SectPos)
    	{
    		if(listFermes.isSelectionEmpty())
    		{
    			JOptionPane.showMessageDialog(this,UICommons.notifFerme);
        		return;
    		}
    		try {
				BDConnect bdc=new BDConnect();
				Secteur tmp=new Secteur(lFermes.get(listFermes.getSelectedIndex()),
						  txtDesignation.getText().trim(),
						  txtDesc.getText().trim(),
						  new ArrayList<LocalisationGPS>());
				tmp.saveDB(bdc);
				
				bdc.shutdown();
				lFermes.get(listFermes.getSelectedIndex()).addSecteur(tmp);
				loadSecteurs();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb secteur saveThis(): "+e.getMessage());
			}
    	}
    	
    	//New Billon option choosen
    	if(typeChamp.getSelectedIndex()==UICommons.BillPos)
    	{
    		if(listSerres.isSelectionEmpty())
    		{
    			JOptionPane.showMessageDialog(this,UICommons.notifSerre);
        		return;
    		}
    		try {
				BDConnect bdc=new BDConnect();
				Billon tmp=new Billon(lSerres.get(listSerres.getSelectedIndex()),
						  txtDesignation.getText().trim(),
						  txtDesc.getText().trim(),
						  null);
				tmp.saveDB(bdc);
				lSerres.get(listSerres.getSelectedIndex()).addBillon(tmp);
				
				bdc.shutdown();
				
				loadBillons();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				functions.affiche("pb billon saveThis(): "+e.getMessage());
			}
    	}
    	JOptionPane.showMessageDialog(this,UICommons.notifSaveSuccess);
    	emptyFields();
    	
    }
    
    /**
     * Checks whether all fields've been filled before submitting the form 
     * @return true if an empty field exist
     * @return false otherwise
     */
    public boolean existEmptyFields()
    {
    	
    	if(txtDesignation.getText().trim().length()==0)
    		return true;
    	if(txtDesc.getText().trim().length()==0)
    		return true;
    	
    	if(typeChamp.getSelectedIndex()==0 && txtAdr.getText().trim().length()==0)
    		return true;
    	return false;
    }
    
    
    /**
     * Empty all the "Champ form"'s fields!!
     */
    public void emptyFields()
    {
    	txtDesignation.setText("");
    	txtDesc.setText("");
    	txtAdr.setText("");
    	//typeChamp.setSelectedIndex(0);
    }
    
    /**
     * Executed method for showing details on chosen champ 
     * when btnValider is clicked
     */
    public void showThis()
    {
    	if((typeChamp.getSelectedIndex()==UICommons.fermPos && listFermes.getSelectedIndex()!=-1)
    	 ||(typeChamp.getSelectedIndex()==UICommons.SerrPos && listSerres.getSelectedIndex()!=-1)
    	 ||(typeChamp.getSelectedIndex()==UICommons.SectPos && listSecteurs.getSelectedIndex()!=-1)
    	 ||(typeChamp.getSelectedIndex()==UICommons.BillPos && listBillons.getSelectedIndex()!=-1))
    	{
    		functions.affiche("showThis()");
    		removeListListeners();
    		holdCurrent();
    		//initComponents();
    		init4Edit();
    		listenListSelectionMode3();
    		mode=3;
        	functions.affiche("mode3");
        	//secteurView();
    	}
    	else
    		JOptionPane.showMessageDialog(this,UICommons.flblNewChpNotif
    				+UICommons.fTypeFields[typeChamp.getSelectedIndex()]);
    	
    }
    
    /**
     * Executed method for editing informations about a chosen champ
     * when btnValider is clicked
     */
    public void editThis()
    {
    	
    	functions.affiche("editThis()");
    	
    	if(existEmptyFields())
    	{
    		JOptionPane.showMessageDialog(this,UICommons.notifEmpty);
    		return;
    	}
    	if(!specifiedParent())
    	{
    		JOptionPane.showMessageDialog(this,UICommons.notifParent+
    				UICommons.fTypeFields[typeChamp.getSelectedIndex()]);
    		return;
    	}
    	
    	//A  Ferme is choosen to be edit!!
    	if(typeChamp.getSelectedIndex()==UICommons.fermPos)
    	{
    		BDConnect bdc;
			try {
				bdc = new BDConnect();
				lFermes.get(listFermes.getSelectedIndex()).updateDB(bdc,
						txtDesignation.getText().trim(),
						txtDesc.getText().trim(),
						txtAdr.getText().trim());
	    		bdc.shutdown();
	    		JOptionPane.showMessageDialog(this,UICommons.notifSaveSuccess);
	    		// TODO Get back to mode2
	    		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb ferme editThis(): "+e.getMessage());
			}
    		
    	}
    	
    	
    	//A  Serre is choosen to be edit!!
    	if(typeChamp.getSelectedIndex()==UICommons.SerrPos)
    	{
    		BDConnect bdc;
			try {
				bdc = new BDConnect();
				lSerres.get(listSerres.getSelectedIndex()).updateDB(bdc,
						txtDesignation.getText().trim(),
						txtDesc.getText().trim(),
						lSecteurs.get(listSecteurs.getSelectedIndex())
						);
	    		bdc.shutdown();
	    		JOptionPane.showMessageDialog(this,UICommons.notifSaveSuccess);
	    		// TODO Get back to mode2
	    		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb serre editThis(): "+e.getMessage());
			}
    		
    	}
    	
    	//A  Secteur is choosen to be edit!!
    	if(typeChamp.getSelectedIndex()==UICommons.SectPos)
    	{
    		BDConnect bdc;
			try {
				bdc = new BDConnect();
				lSecteurs.get(listSecteurs.getSelectedIndex()).updateDB(bdc,
						txtDesignation.getText().trim(),
						txtDesc.getText().trim(),
						lFermes.get(listFermes.getSelectedIndex())
						);
	    		bdc.shutdown();
	    		JOptionPane.showMessageDialog(this,UICommons.notifSaveSuccess);
	    		// TODO Get back to mode2
	    		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb secteur editThis(): "+e.getMessage());
			}
    		
    	}
    	
    	//A Billon is choosen to be edit!!
    	if(typeChamp.getSelectedIndex()==UICommons.BillPos)
    	{
    		BDConnect bdc;
			try {
				bdc = new BDConnect();
				lBillons.get(listBillons.getSelectedIndex()).updateDB(bdc,
						txtDesignation.getText().trim(),
						txtDesc.getText().trim(),
						lSerres.get(listSerres.getSelectedIndex())
						);
	    		bdc.shutdown();
	    		JOptionPane.showMessageDialog(this,UICommons.notifSaveSuccess);
	    		// TODO Get back to mode2
	    		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb billon editThis(): "+e.getMessage());
			}
    		
    	}
    	mode=2;
    	emptyFields();
    	typeChamp.setSelectedIndex(UICommons.fermPos);
    	typeChamp.setEnabled(true);
    	removeListListeners();
    	listenListSelectionMode2();
    	//serreView();
    	init4Display();
    	loadFermes();
    	releaseLists();
    }
    
    
    /**
     * Executed method for deleting a chosen champ
     * when btnValider is clicked
     */
    public void deleteThis()
    {
    	functions.affiche("del mode");
    	if(typeChamp.getSelectedIndex()==UICommons.fermPos &&
    			listFermes.getSelectedIndex()!=-1)
    	{
    		try {
    			BDConnect bdc=new BDConnect();
				BDFunctions.deleteFerme(lFermes.get(
						listFermes.getSelectedIndex()).getIdEspaceTravail(), bdc);
				bdc.shutdown();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb del: "+e.getMessage());
			}
    		JOptionPane.showMessageDialog(this,UICommons.notifDelSuccess);
    		loadFermes();
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.SerrPos &&
    			listSerres.getSelectedIndex()!=-1)
    	{
    		try {
    			BDConnect bdc=new BDConnect();
    			int pos=listSerres.getSelectedIndex();
    			Serre tmp=lSerres.get(pos);
				BDFunctions.deleteSerre(lSerres.get(pos).getIdEspaceTravail(), bdc);
				bdc.shutdown();
				lSecteurs.get(listSecteurs.getSelectedIndex()).removeSerre(tmp);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb del: "+e.getMessage());
			}
    		JOptionPane.showMessageDialog(this,UICommons.notifDelSuccess);
    		loadSerres();
    	}
    	else if(typeChamp.getSelectedIndex()==UICommons.SectPos &&
    			listSecteurs.getSelectedIndex()!=-1)
    	{
    		try {
    			BDConnect bdc=new BDConnect();
    			int pos=listSecteurs.getSelectedIndex();
    			Secteur tmp=lSecteurs.get(pos);
				BDFunctions.deleteSecteur(lSecteurs.get(pos).getIdEspaceTravail(), bdc);
				bdc.shutdown();
				lFermes.get(listFermes.getSelectedIndex()).removeSecteur(tmp);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb del: "+e.getMessage());
			}
    		JOptionPane.showMessageDialog(this,UICommons.notifDelSuccess);
    		loadSecteurs();
    	}
    	
    	else if(typeChamp.getSelectedIndex()==UICommons.BillPos &&
    			listBillons.getSelectedIndex()!=-1)
    	{
    		try {
    			BDConnect bdc=new BDConnect();
    			int pos=listBillons.getSelectedIndex();
    			Billon tmp=lBillons.get(pos);
				BDFunctions.deleteBillon(tmp.getIdEspaceTravail(), bdc);
				bdc.shutdown();
				lSerres.get(listSerres.getSelectedIndex()).removeBillon(tmp);
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				functions.affiche("pb del: "+e.getMessage());
			}
    		JOptionPane.showMessageDialog(this,UICommons.notifDelSuccess);
    		loadBillons();
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this,UICommons.flblNewChpNotif
    				+UICommons.fTypeFields[typeChamp.getSelectedIndex()]);
    		return;
    	}
    	    	
    	    		
    }
    
    /**
     * Cleans the UI when btnAnnuler is clicked
     */
    public void cleanThis()
    {
    	
    }
    
    
    /**
     * Set listSelection Listener when in mode 1; just consist of displayin
     * relative objects from parents to children
     */
    public void listenListSelectionMode1()
    {
    	listFermes.addListSelectionListener(new ListSelectionListener()
        {

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// TODO Auto-generated method stub
				loadSecteurs();
			}
        	
        });
        
        listSerres.addListSelectionListener(new ListSelectionListener()
        {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				loadBillons();
			}
        	
        });
        
        
        listSecteurs.addListSelectionListener(new ListSelectionListener()
        {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				//functions.affiche("load serres!!");
				loadSerres();
			}
        	
        });

    }
    
    
    /**
     * listSelection Listener when in mode 2; In addition here datas are loaded;
     */
    public void listenListSelectionMode2()
    {
    	
    	listFermes.addListSelectionListener(new ListSelectionListener()
    	{
    		

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// TODO Auto-generated method stub
				
				//listSerres.setSelectedIndex(0);
				displayFerme();
				//listSerres.setModel(new DefaultListModel());
				loadSecteurs();
				//functions.affiche("serres loaded: "+lSerres.size());
			}
    		
    	});
    	listSerres.addListSelectionListener(new ListSelectionListener()
    	{

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// TODO Auto-generated method stub
				if(listSerres.getSelectedIndex()!=-1)
				{
					loadBillons();
					displaySerre();
				}
				
			}
    		
    	});
    	
    	listSecteurs.addListSelectionListener(new ListSelectionListener()
    	{

			@Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				if(listSecteurs.getSelectedIndex()!=-1)
				{
					displaySecteur();
					loadSerres();
				}
				
			}
    		
    	});
    	
    	listBillons.addListSelectionListener(new ListSelectionListener()
    	{

			@Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				if(listBillons.getSelectedIndex()!=-1)
				{
					displayBillon();
				}
				
			}
    		
    	});
    }
    
    
    
    /**
     * Load the chosen ferme datas in the form below
     */
    public void displayFerme()
    {
    	//functions.affiche("disp Ferme.......");
    	if(typeChamp.getSelectedIndex()==UICommons.fermPos)
    	{
    		jLabel5.setVisible(true);
        	txtAdr.setVisible(true);
    	}
    	Ferme tmp=lFermes.get(listFermes.getSelectedIndex());
    	txtDesignation.setText(tmp.getNomEspaceTravail());
    	txtDesc.setText(tmp.getDescriptionEspaceTravail());
    	txtAdr.setText(tmp.getAdresseFerme());
    	//typeChamp.setSelectedIndex(0);
    }
    
    /**
     * Load the chosen serre datas in the form below
     */
    public void displaySerre()
    {
    	jLabel5.setVisible(false);
    	txtAdr.setVisible(false);
    	Serre tmp=lSerres.get(listSerres.getSelectedIndex());
    	txtDesignation.setText(tmp.getNomEspaceTravail());
    	txtDesc.setText(tmp.getDescriptionEspaceTravail());
    	//typeChamp.setSelectedIndex(1);
    }
    
    /**
     * Load the chosen secteur datas in the form below
     */
    public void displaySecteur()
    {
    	jLabel5.setVisible(false);
    	txtAdr.setVisible(false);
    	Secteur tmp=lSecteurs.get(listSecteurs.getSelectedIndex());
    	txtDesignation.setText(tmp.getNomEspaceTravail());
    	txtDesc.setText(tmp.getDescriptionEspaceTravail());
    	//typeChamp.setSelectedIndex(2);
    }
    
    /**
     * Load the chosen billon datas in the form below
     */
    public void displayBillon()
    {
    	jLabel5.setVisible(false);
    	txtAdr.setVisible(false);
    	Billon tmp=lBillons.get(listBillons.getSelectedIndex());
    	txtDesignation.setText(tmp.getNomEspaceTravail());
    	txtDesc.setText(tmp.getDescriptionEspaceTravail());
    
    }
    
    /**
     * enable or disable the lists, so that user can't modify the parent of 
     * the champ they're about edit 
     */
    public void holdCurrent()
    {
    	if(typeChamp.getSelectedIndex()==UICommons.fermPos)
    	{
    		listFermes.setEnabled(false);
        	listSerres.setEnabled(false);
        	listSecteurs.setEnabled(false);
        	listBillons.setEnabled(false);
        	
    	}
    	
    	if(typeChamp.getSelectedIndex()==UICommons.SectPos)
    	{
    		listFermes.setEnabled(true);
        	listSerres.setEnabled(false);
        	listSecteurs.setEnabled(false);
        	listBillons.setEnabled(false);
    	}
    	
    	if(typeChamp.getSelectedIndex()==UICommons.SerrPos)
    	{
    		listFermes.setEnabled(true);
        	listSecteurs.setEnabled(true);
        	listSerres.setEnabled(false);
        	listBillons.setEnabled(false);
    	}
    	
    	if(typeChamp.getSelectedIndex()==UICommons.BillPos)
    	{
    		listFermes.setEnabled(true);
        	listSecteurs.setEnabled(true);
        	listSerres.setEnabled(true);
        	listBillons.setEnabled(false);
        	
        	
    	}
    	
    }
    
    public void removeListListeners()
    {
    	if(listSecteurs.getListSelectionListeners().length!=0)
    	{
    		listSecteurs.removeListSelectionListener(
    				listSecteurs.getListSelectionListeners()[0]);
    	}
    	if(listSerres.getListSelectionListeners().length!=0)
    	{
    		listSerres.removeListSelectionListener(
    				listSerres.getListSelectionListeners()[0]);
    	}
    	if(listFermes.getListSelectionListeners().length!=0)
    	{
    		listFermes.removeListSelectionListener(
    				listFermes.getListSelectionListeners()[0]);
    	}
    }
    
    
    /**
     * Used in edition mode
     */
    public void listenListSelectionMode3()
    {
    	//functions.affiche("yep");
    	removeListListeners();
    	if(typeChamp.getSelectedIndex()==UICommons.BillPos)
    	{
    		listFermes.addListSelectionListener(new ListSelectionListener()
            {
        		
    			@Override
    			public void valueChanged(ListSelectionEvent arg0) {
    				// TODO Auto-generated method stub
    				loadSecteurs3();
    			}
            	
            });
            
            listSecteurs.addListSelectionListener(new ListSelectionListener()
            {

    			@Override
    			public void valueChanged(ListSelectionEvent e) {
    				// TODO Auto-generated method stub
    				loadSerres3();
    			}
            	
            });
    	}
    	if(typeChamp.getSelectedIndex()==UICommons.SerrPos)
    	{
    		listFermes.addListSelectionListener(new ListSelectionListener()
            {
        		
    			@Override
    			public void valueChanged(ListSelectionEvent arg0) {
    				// TODO Auto-generated method stub
    				loadSecteurs3();
    			}
            	
            });
    	}
    	
    }
    
    /**
     * used to check whether the form has been well filled in, while editing!! 
     * For a Billon for example, the user would may be choose to change th "secteur"
     * parent. We should make sure, there's one selected!!
     */
    public boolean specifiedParent()
    {
    	if((typeChamp.getSelectedIndex()==UICommons.fermPos)
    	 ||(typeChamp.getSelectedIndex()==UICommons.SectPos && listFermes.getSelectedIndex()!=-1)
    	 ||(typeChamp.getSelectedIndex()==UICommons.SerrPos && listSecteurs.getSelectedIndex()!=-1)
    	 ||(typeChamp.getSelectedIndex()==UICommons.BillPos && listSerres.getSelectedIndex()!=-1))
    		return true;
    	return false;
    	
    }
    
    /**
     * Enable all Lists
     */
    public void releaseLists()
    {
    	listFermes.setEnabled(true);
    	jLabel7.setEnabled(true);
    	listSerres.setEnabled(true);
    	jLabel8.setEnabled(true);
    	listBillons.setEnabled(true);
    	jLabel9.setEnabled(true);
    	listSecteurs.setEnabled(true);
    	jLabel10.setEnabled(true);
    }
                
    
}

